package club.baimahu.pencil.domain.entity.account;

import club.baimahu.pencil.annotation.Ordered;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Data
public class SimpleAccountManager extends AbstractAccountManager{

    Logger logger = LoggerFactory.getLogger(SimpleAccountManager.class);

    List<Account> accountList;

    BigDecimal totalBalance;

    String accountManagerName;


    public BigDecimal getBalance(){
        return this.totalBalance;
    }

    public SimpleAccountManager(){}
    public SimpleAccountManager(String accountManagerName ,Account account){
        this.accountManagerName = accountManagerName;
        addAccount(account);
    }


    public boolean canAfford(BigDecimal bigDecimal){
        Assert.isTrue(bigDecimal.doubleValue() >= 0,"bigDecimal is not legal");
        return totalBalance.doubleValue()> bigDecimal.doubleValue() ;
    }

    public synchronized void decrease(BigDecimal bigDecimal){
        Assert.isTrue(bigDecimal.doubleValue()>=0,"bigDecimal is illegal");
        Assert.isTrue(totalBalance.doubleValue()>bigDecimal.doubleValue(),"balance cannot afford");

        accountList.stream().sorted(Comparator.comparingInt(Ordered::getOrder))
                .findFirst()
                .ifPresent(x-> {
                    x.decrease(bigDecimal);
                    logger.info("账户【{}】 支出【{}】元",x.accountName(),bigDecimal);
                });
        totalBalance = totalBalance.subtract(bigDecimal);
        logger.info("总账户【{}】支出【{}】元,账户余额【{}】",this.getAccountManagerName(),bigDecimal,totalBalance);
    }


    public synchronized void increase(BigDecimal bigDecimal){
        Assert.isTrue(bigDecimal.doubleValue()>=0,"bigDecimal is illegal");

        accountList.stream().sorted(Comparator.comparingInt(Ordered::getOrder))
                .findFirst()
                .ifPresent(x->{
                    x.increase(bigDecimal);
                    logger.info("账户【{}】增加{}元",x.accountName(),bigDecimal);

                });
        totalBalance = totalBalance.add(bigDecimal);
        logger.info("总账户【{}】增加收入【{}】元,账户余额:【{}】元",this.getAccountManagerName(),bigDecimal,totalBalance);

    }


    @Override
    public boolean pay(BigDecimal money, Account account) {
        logger.info("正在付款给【{}】【{}】元",account.accountName(),money.doubleValue());
        decrease(money);
        return true;
    }

    @Override
    public boolean income(BigDecimal money, Account account) {
        logger.info("账户【{}】增加余额【{}】元，打款账户：【{}】",this.getAccountManagerName(),money.doubleValue(),account.accountName());
        increase(money);
        return true;
    }

    @Override
    public Account getIncomeAccount() {
        return this.accountList.get(0);
    }

    @Override
    public Account getPayAccount() {
        return this.accountList.get(0);
    }


    @Override
    public void addAccount(Account account) {

        if(this.accountList == null){
            this.accountList = new ArrayList<>();
        }
        if(this.totalBalance == null){
            this.totalBalance = BigDecimal.ZERO;
        }

        this.accountList.add(account);
        //按付款优先顺序
        this.accountList.sort(Comparator.comparingInt(Ordered::getOrder));
        this.totalBalance = this.totalBalance.add(account.balance());

    }

    @Override
    public String accountManagerName() {
        return this.accountManagerName;
    }
}
